// 交易方向
const TradeStrategy = {
  SELL: 0,
  BUY: 1,
  NOT_DEAL: 2,
  NO_STRATEGY: 3,
};

// 申报电量类型
const SubmitType = {
  FIXED: 0, // 固定值
  INSTALLED_CAPACITY: 1, // 装机容量
  TRADE_LIMIT: 2, // 交易限额
};

const TradeStrategyMap = {
  0: '卖出',
  1: '买入',
  2: '不交易',
};

const rules = {
  keepTwoDecimalPlaces: {
    reg: /^([0-9]{1}|^[1-9]{1}\d{1,15})(\.\d{1,2})?$/,
    msg: '只能是数字类型且可保留两位小数',
  },
  keepThreeDecimalPlaces: {
    reg: /^([0-9]{1}|^[1-9]{1}\d{1,15})(\.\d{1,3})?$/,
    msg: '只能是数字类型且可保留三位小数',
  },
  keepOneDecimalPlaces: {
    reg: /^([0-9]{1}|^[1-9]{1}\d{1,15})(\.\d{1,1})?$/,
    msg: '只能是数字类型且可保留一位小数',
  },
};

const publicRule = (value, name, type) => {
  let err = '';
  const currentReg = rules[type]['reg'];
  const currentMsg = rules[type]['msg'];
  if (!isEmpty(value)) {
    if (!currentReg.test(value)) {
      err = `${name}${currentMsg}`;
    }
  } else {
    err = `${name}不能为空`;
  }
  return err;
};

// 判断是否为空 数字0也算有效
const isEmpty = (data) => {
  let flag = true;
  if (data === 0) {
    flag = false;
  } else {
    if (data && data !== '-') {
      flag = false;
    }
  }
  return flag;
};

// 除山东之外
const notSdImportMapping = {
  __EMPTY: {
    translate: {
      卖出: TradeStrategy.SELL,
      买入: TradeStrategy.BUY,
      不交易: TradeStrategy.NOT_DEAL,
      无策略: TradeStrategy.NO_STRATEGY,
    },
    key: 'tradeStrategy',
    rule: (value, rowData) => {
      let err = '';
      if (!value) {
        err = `请填写交易方向`;
      } else {
        let arr = ['卖出', '买入', '不交易', '无策略'];
        if (!arr.includes(value)) {
          err = `交易方向只能是${arr.join(',')}`;
        }
      }
      return err;
    },
  },
  __EMPTY_1: {
    key: 'upperPrice',
    rule: (value, rowData) => publicRule(value, '价格上限', 'keepTwoDecimalPlaces'),
  },
  __EMPTY_2: {
    key: 'lowerPrice',
    rule: (value, rowData) => publicRule(value, '价格下限', 'keepTwoDecimalPlaces'),
  },
  __EMPTY_3: {
    key: 'buyLimit',
    rule: (value, rowData) => publicRule(value, '买入限额', 'keepThreeDecimalPlaces'),
  },
  __EMPTY_4: {
    key: 'sellLimit',
    rule: (value, rowData) => publicRule(value, '卖出限额', 'keepThreeDecimalPlaces'),
  },
  __EMPTY_5: {
    key: 'submitType',
    fn: (row) => {
      let submitType = undefined;
      let firstSubmitPower = undefined;
      if (!isEmpty(row.__EMPTY_7)) {
        submitType = SubmitType.FIXED;
        firstSubmitPower = Number(row.__EMPTY_7);
      } else {
        firstSubmitPower = Number(row.__EMPTY_6) / 100;
        submitType =
          row.__EMPTY_5.indexOf('交易限额') > -1
            ? SubmitType.TRADE_LIMIT
            : SubmitType.INSTALLED_CAPACITY;
      }

      return {
        submitType,
        firstSubmitPower,
      };
    },
    rule: (value, rowData) => {
      let err = '';
      if (isEmpty(rowData.__EMPTY_7)) {
        if (!value || value === '-') {
          err = `段一申报电量中参考值不能是空`;
        } else {
          if (`${value}`.indexOf('交易限额') < 0 && `${value}`.indexOf('装机容量') < 0) {
            err = `参考值只能是交易限额或装机容量`;
          }
        }
      }

      return err;
    },
  },
  __EMPTY_6: {
    rule: (value, rowData) => {
      let err = '';
      if (!isEmpty(value)) {
        if (!rules['keepOneDecimalPlaces']['reg'].test(value)) {
          err = `比例${rules['keepOneDecimalPlaces']['msg']}`;
        } else {
          if (value > 50) {
            err = '比例值不能超过50';
          }
        }
      } else {
        if (isEmpty(rowData.__EMPTY_7)) {
          err = '段一申报电量中的比例和固定值只能有一个是空';
        }
      }
      return err;
    },
  },
  __EMPTY_7: {
    rule: (value, rowData) => {
      let err = '';
      if (!isEmpty(value)) {
        if (!rules['keepThreeDecimalPlaces']['reg'].test(value)) {
          err = '固定值格式错误';
        }
      }
      return err;
    },
  },
  __EMPTY_8: {
    key: 'firstPrice',
    rule: (value, rowData) => publicRule(value, '段一申报电价', 'keepTwoDecimalPlaces'),
  },
  // 以下可以不校验 并没有用输入的值
  __EMPTY_9: {},
  __EMPTY_10: {},
  __EMPTY_11: {},
  __EMPTY_12: {
    key: 'secondPrice',
    fn: (row) => {
      let secondPrice = '';
      if (row.__EMPTY_8 && row.__EMPTY_8 !== '-') {
        secondPrice = row.__EMPTY_8;
      }
      return {
        secondPrice,
      };
    },
  },
  __EMPTY_13: { key: 'totalPower' },
};

// 山东
const sdImportMapping = {
  __EMPTY: {
    translate: {
      卖出: TradeStrategy.SELL,
      买入: TradeStrategy.BUY,
      不交易: TradeStrategy.NOT_DEAL,
    },
    key: 'tradeStrategy',
    rule: (value, rowData) => {
      let err = '';
      if (!value) {
        err = `请填写交易方向`;
      } else {
        let arr = ['卖出', '买入', '不交易'];
        if (!arr.includes(value)) {
          err = `交易方向只能是${arr.join(',')}`;
        }
      }
      return err;
    },
  },
  __EMPTY_1: {
    key: 'upperPrice',
    rule: (value, rowData) => publicRule(value, '价格上限', 'keepTwoDecimalPlaces'),
  },
  __EMPTY_2: {
    key: 'lowerPrice',
    rule: (value, rowData) => publicRule(value, '价格下限', 'keepTwoDecimalPlaces'),
  },
  __EMPTY_3: {
    key: 'submitType',
    rule: (value, rowData) => {
      let err = '';
      if (isEmpty(rowData.__EMPTY_5)) {
        if (!value || value === '-') {
          err = `参考值不能是空`;
        } else {
          if (`${value}`.indexOf('装机容量') < 0) {
            err = `参考值只能是装机容量`;
          }
        }
      }
      return err;
    },
    fn: (row) => {
      let submitType = undefined;
      let firstSubmitPower = undefined;
      if (!isEmpty(row.__EMPTY_5)) {
        submitType = SubmitType.FIXED;
        firstSubmitPower = Number(row.__EMPTY_5);
      } else {
        firstSubmitPower = Number(row.__EMPTY_4) / 100;
        submitType = SubmitType.INSTALLED_CAPACITY;
      }
      return {
        submitType,
        firstSubmitPower,
      };
    },
  },
  __EMPTY_4: {
    rule: (value, rowData) => {
      let err = '';
      if (!isEmpty(value)) {
        if (!rules['keepOneDecimalPlaces']['reg'].test(value)) {
          err = `比例${rules['keepOneDecimalPlaces']['msg']}`;
        } else {
          if (value > 100) {
            err = '比例值不能超过100';
          }
        }
      } else {
        if (isEmpty(rowData.__EMPTY_5)) {
          err = '段一申报电量中的比例和固定值只能有一个是空';
        }
      }
      return err;
    },
  },
  __EMPTY_5: {
    rule: (value, rowData) => {
      let err = '';
      if (!isEmpty(value)) {
        if (!rules['keepThreeDecimalPlaces']['reg'].test(value)) {
          err = '固定值格式错误';
        }
      }
      return err;
    },
  },
  __EMPTY_6: {
    key: 'firstPrice',
    rule: (value, rowData) => publicRule(value, '申报电价', 'keepTwoDecimalPlaces'),
  },
  __EMPTY_7: {},
};

const importConfig = (isSd) => {
  return !isSd ? notSdImportMapping : sdImportMapping;
};

export { TradeStrategy, SubmitType, TradeStrategyMap, importConfig };
